//父进程调用wait函数完成对子进程的回收
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include <unistd.h>
#include<sys/wait.h>



int main(int argc,char* argv[])
{
	//创建子进程
	//pid_t fork(void);
	int status;
	pid_t pid = fork();
	if(pid<0)//fork失败的情况
	{
		perror("fork error");
		return -1;
	}
	else if(pid > 0)
	{
		printf("father,pid = [%d],ppid = [%d]\n",getpid(),getppid());
		//pid_t wpid = wait(NULL);
		//pid_t wpid = waitpid(pid,&status,0);
		sleep(25);
		pid_t wpid = waitpid(-1,&status,WNOHANG);//-1表示等待任意一个子进程，WNOHANG表示不阻塞
		printf("wpid=[%d]\n",wpid);
		if(WIFEXITED(status))//正常退出`
		{
			printf("child normal exit,status==[%d]\n",WEXITSTATUS(status));
		}
		if(WIFSIGNALED(status))//被信息杀死
		{
			printf("child be Kill,status==[%d]\n",WTERMSIG(status));
		}
	}
	else if(pid == 0)
	{	
		printf("child,pid = [%d],ppid = [%d]\n",getpid(),getppid());
		return 9;
	}
	return 0;
}
